<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/19
 * Time: 13:54
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Api\V1\App\Customers;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Customers\ResetPwdRequest;
use App\Models\Customers\Customer;
use App\Services\Customers\ResetPwd\ResetPwdFactory;
use App\Services\EasySms\EasySmsService;

class ResetPasswordController extends ApiController
{
    /**
     * @param ResetPwdRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function resetPwd(ResetPwdRequest $request)
    {
        // todo 直接给客户改密码，逻辑有问题，万一客户没有收到新密码呢。
        $customer = new Customer;
        $customer->email = $request->email;
        $customer->telephone = $request->telephone;
        $customer->dial_code = $request->dial_code ?: EasySmsService::COUNTRY_CODE_CN;
        $customer->verify_code = $request->verify_code;

        $service = ResetPwdFactory::create($request->type);

        $reset_pwd = $service->resetPwd($customer);

        return $this->response(compact('reset_pwd'));
    }
}